<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class SystemConfigController extends AdminbaseController{
	public function index(){
		$System_config      = M('System_config');
        $system_config_list = $System_config->order('field,id asc')->select();

        foreach ($system_config_list as $key => $val) {
        	$new_system_config_list[$val['field']][] = $val; 
        }
  
        $this->assign('system_config_list',$new_system_config_list);
        $this->display();
	}

	//添加配置
	public function add(){
	    $System_config = M('System_config');
		if(IS_POST){
			$name  = I('post.name');
			$field = I('post.field');
			$key   = I('post.key');
			$value = I('post.value');
		
			foreach ($key as $k => $v) {
				$system_config_list[$k]['name']  = $name;
				$system_config_list[$k]['field'] = $field;
				$system_config_list[$k]['key']   = $v;
				$system_config_list[$k]['value'] = $value[$k]; 
			}
            $result = $System_config->addAll($system_config_list);
            if($result){
            	$this->success("添加成功！", U("SystemConfig/index"));
            }
		}
		$this->display();
	}

	//添加配置项
	public function add_item(){
		$add_item['name']  = I('post.name');
		$add_item['field'] = I('post.field');
		$add_item['key']   = I('post.key');
		$add_item['value'] = I('post.value');
		$data['status'] = 0;
		if(M('System_config')->add($add_item)){
			$data['status'] = 1;
		}
		$this->ajaxReturn($data);
	}

	//配置编辑
	public function edit(){
		$System_config      = M('System_config');

		$id = I('post.id');
		$update['key']   = I('post.key');
		$update['value'] = I('post.value');
        
        $result = $System_config->where('id = '.$id)->save($update);
        $data['status'] = 0;
		if($result || $result === 0){
			$data['status'] = 1;
		}
		$this->ajaxReturn($data);

	}

	//配置删除
	public function delete(){
		$System_config = M('System_config');
		$where['id']   = intval($_GET['id']);
		$data['status'] = 0;
		if($System_config->where($where)->delete()){
			$data['status'] = 1;
		}
		$this->ajaxReturn($data);
	}
}